import {observable, action} from 'mobx';
import {ListPage} from 'weaver-mobile-page';
import {Toast,Tools} from 'weaver-mobile';
import * as API from '../../api/cptApi'
const getLabel = Tools.getLabel;

export default class HomeStore {
    @observable sum = 0;
    @observable  fetch_data = 0;//领用
    @observable lend_data = 0;//借用
    @observable instock_data = 0;//待验收
    @observable inventory_data = 0;//待盘点

    @observable visualHeight;
    @observable refreshing = false;
    @observable dirDown = true;

    @observable myAssetData = new ListPage.Store();
    @observable instockData = new ListPage.Store();

    @observable isright1 = false;
    @observable isright2 = false;
    @observable refresh = 0;
    @observable options = [];

    @action
    init = () => {
        this.getPortalCount();
        this.getPortalMyCapital();
        this.getPortalInstock();
    };
    @action
    setVisualHeight = (h) => {
        this.visualHeight = h;
    };

    @action
    handleRefreshing = (bool) => {
        this.refreshing = bool;
    };

    @action
    getPortalInstock = (params) => {        //待验收资
        API.getPortalInstock(params).then(res => {
            const {isright, datas} = res;
            if (isright) {
                this.isright1 = isright;
                this.instockData.setState({data: datas});
            } else {
                this.isright1 = false;
            }
        });
    };

    @action
    getPortalMyCapital = (params) => {      //我的资产
        API.getPortalMyCapital(params).then(res => {
            const {isright, datas} = res;
            if (isright) {
                this.isright2 = isright;
                this.myAssetData.setState({data: datas});
            } else {
                this.isright2 = false;
            }
        });
    }

    @action
    doInstockDelete = (id) => {//条目作废
        API.doInstockDelete(id).then((res) => {
            if (res.flag) {
                Toast.success(getLabel(389850,'该条目已作废'), 2);
                this.init();
            } else {
                Toast.fail(getLabel(30651,'操作失败'), 2);
            }
        })
    }

    @action
    getPortalCount = () => {
        API.getPortalCount().then((res) => {
            const {sum,inventory_data, instock_data, fetch_data, lend_data} = res;
            // this.sum = parseInt(inventory_data, 10) + parseInt(instock_data, 10) +
            //     parseInt(fetch_data, 10) + parseInt(lend_data, 10);
            this.sum = sum;
            this.refresh++;
            this.options = [{
                title: getLabel(1378,'领用'),
                name: 'fetch_data',
                colorBegin: '#1c6a04',
                colorEnd: '#2cd615',
                num: fetch_data
            },
                {
                    title: getLabel(15325,'借用'),
                    name: 'lend_data',
                    colorBegin: '#6c0484',
                    colorEnd: '#de4df3',
                    num: lend_data
                },

                {
                    title: getLabel(389833,'待验收'),
                    name: 'instock_data',
                    colorBegin: '#9b9b9b',
                    colorEnd: '#dddddd',
                    num: instock_data
                },
                {
                    title: getLabel(384644,'待盘点'),
                    name: 'inventory_data',
                    colorBegin: '#ff5b05',
                    colorEnd: '#eec149',
                    num: inventory_data
                }]
        })
    }
    @action
    onBack = (capitalid, method) => {
        let params = {
            mtype: 'cptback',
            capitalid: capitalid
        };
        if (method) {
            params = {
                ...params,
                method: 'backMyCpt'
            }
        }
        Toast.loading('归还中...',30);
        API.doCptManager(params).then(res => {
            Toast.hide();
            if (res.msg === '') {
                Toast.success(getLabel(30700,'操作成功'), 2);
                this.init();
            } else {
                Toast.fail(res.msg, 2);
            }
        })
    };
    @action
    onDelete = (id) => {
        const params = {
            capitalid: id
        };
        Toast.loading('删除中...',30);
        API.doCapitalDel(params).then(res => {
            Toast.hide();
            if (res.flag) {
                Toast.success(getLabel(20461,'删除成功'), 1);
                this.init();
            } else {
                Toast.fail(res.msg,2);
            }
        })
    }
    @action
    onRepair = (history, item) => {
        history.push(`/operations/cptmend?cptid=${item.id}&cptname=${item.name}`)
    }

}
